import { ref } from 'vue'
import { io } from 'socket.io-client'
import { Socket } from 'socket.io-client'
const roomId = ref('fanjiantao')
const userId = 'fanjiantao@' + Math.random().toString(16).slice(2)
const toUser = 'fanjiantao2@' + Math.random().toString(16).slice(2)
const logger = (name, log) => {
  console.log('-----------' + name + '@satrt-----------')
  console.log(log)
  console.log('-----------' + name + '@end-----------')
  console.log('\n')
}

const createOffer = async (socket: Socket, pc) => {
  // const offer = await pc.createOffer({
  //   offerToReceiveAudio: false,
  //   offerToReceiveVideo: false
  // })
  const offer = await pc.createOffer()
  logger('pc-createOffer-success', offer.sdp)
  await pc.setLocalDescription(offer)
  logger('pc-setLocalDescription-success', '设置成功local')
  // pc.onconnectionstatechange = (e) => {
  //   logger('pc-connectionstatechange', e)
  // }
  pc.onicecandidate = async (e) => {
    // logger('pc-icecandidate', e.candidate?.candidate)
    if (e.candidate) {
      // await pc.addIceCandidate(e.candidate)
      socket.emit('icecandidate', {
        to: toUser,
        from: userId,
        roomId: roomId.value,
        candidate: e.candidate
      })
      // logger('pc.addIceCandidate-sucess', 'pc 设置Candidate成功')
    }
  }

  socket.emit('offer', { to: toUser, from: userId, roomId: roomId.value, offer: offer })

  //* 发送给pc
  // createAnswer(offer)
}

const createAnswer = async (desc, pc: RTCPeerConnection, socket) => {
  // pc.oniceconnectionstatechange = (e) => {
  //   logger('pc-iceconnectionstatechange', e)
  // }
  pc.onicecandidate = async (e) => {
    // logger('pc-icecandidate', e.candidate?.candidate)
    if (e.candidate) {
      // await pc.addIceCandidate(e.candidate)
      socket.emit('icecandidate', {
        to: toUser,
        from: userId,
        roomId: roomId.value,
        candidate: e.candidate
      })
      // logger('pc.addIceCandidate-sucess', 'pc 设置Candidate成功')
    }
  }
  pc.ontrack = (e) => {
    logger('pc.ontrack', e.streams[0])
  }
  await pc.setRemoteDescription(desc)
  logger('pc-setRemoteDescription-success', desc)
  const answer = await pc.createAnswer()
  logger('pc.createAnswer-success', answer)
  await pc.setLocalDescription(answer)
  logger('pc.setLocalDescription-success', '设置成功')
  socket.emit('answer', { to: toUser, form: userId, answer: answer, roomId: roomId.value })
}

const addAnswer = async (answer, pc: RTCPeerConnection) => {
  await pc.setRemoteDescription(answer)
}

export const useRtcFIle = (
  url: string,
  roomId: string
): {
  socket: Socket
  pc: RTCPeerConnection
  createOffer: () => Promise<void>
} => {
  const socket = io(url)
  socket.connect()
  const pc = new RTCPeerConnection()
  socket.on('connect', () => {
    socket.emit('join', { roomId: roomId, userId: userId })
    socket.on('offer', (data) => {
      logger('socket-offer', data)
      createAnswer(data.offer, pc, socket)
    })
    socket.on('answer', (data) => {
      addAnswer(data.answer, pc)
    })

    socket.on('icecandidate', async (data) => {
      await pc.addIceCandidate(data.candidate)
      logger('pc.addIceCandidate-sucess', 'pc 设置Candidate成功')
    })
  })
  return {
    socket,
    pc,
    createOffer: async () => {
      //* 发起
      await createOffer(socket, pc)
    }
  }
}
